package com.ruoyi.am.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.am.domain.AmDriver;
import com.ruoyi.am.domain.AmDriving;
import com.ruoyi.am.domain.AmTrip;
import com.ruoyi.am.service.*;
import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.am.domain.AmWork;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作量Controller
 * 
 * @author hkkk
 * @date 2021-01-15
 */
@Controller
@EnableScheduling
@Configuration
@RequestMapping("/am/work")
public class AmWorkController extends BaseController
{
    private String prefix = "am/work";

    @Autowired
    private IAmWorkService amWorkService;
    @Autowired
    private IAmDrivingService amDrivingService;
    @Autowired
    private IAmDriverService amDriverService;
    @Autowired
    private IAmRosterService amRosterService;
    @Autowired
    private IAmOverService amOverService;
    @Autowired
    private IAmTripService amTripService;

    @RequiresPermissions("am:work:view")
    @GetMapping()
    public String work()
    {
        return prefix + "/work";
    }

    /**
     * 查询工作量列表
     */
    @RequiresPermissions("am:work:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmWork amWork)
    {
        amWork.setMonth(new Date().getMonth()+1+"");
        startPage();
        List<AmWork> list = amWorkService.selectAmWorkList(amWork);
        return getDataTable(list);
    }

    /**
     * 导出工作量列表
     */
    @RequiresPermissions("am:work:export")
    @Log(title = "工作量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmWork amWork)
    {
        List<AmWork> list = amWorkService.selectAmWorkList(amWork);
        ExcelUtil<AmWork> util = new ExcelUtil<AmWork>(AmWork.class);
        return util.exportExcel(list, "work");
    }

    /**
     * 新增工作量
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工作量
     */
    @RequiresPermissions("am:work:add")
    @Log(title = "工作量", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmWork amWork)
    {
        return toAjax(amWorkService.insertAmWork(amWork));
    }

    /**
     * 修改工作量
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AmWork amWork = amWorkService.selectAmWorkById(id);
        mmap.put("amWork", amWork);
        return prefix + "/edit";
    }

    /**
     * 修改保存工作量
     */
    @RequiresPermissions("am:work:edit")
    @Log(title = "工作量", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmWork amWork)
    {
        return toAjax(amWorkService.updateAmWork(amWork));
    }

    /**
     * 删除工作量
     */
    @RequiresPermissions("am:work:remove")
    @Log(title = "工作量", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amWorkService.deleteAmWorkByIds(ids));
    }

//    @Scheduled(cron = "0 0 0 * * ?")
    public void createWork(){
        List<AmWork> old=amWorkService.selectAmWorkList(new AmWork());
        for(AmWork amWork:old){
            if(amWork.getMonth().equals(new Date().getMonth()+1+""))
               amWorkService.deleteAmWorkById(amWork.getId());
        }
        List<AmWork> data=calculateWork();
        for(AmWork amWork:data){
            amWork.setCreateTime(DateUtils.getNowDate());
            amWorkService.insertAmWork(amWork);
        }
    }

    public List<AmWork> calculateWork(){
        List<AmDriver> drivers=amDriverService.selectAmDriverList(new AmDriver());
        List<AmWork> works=new ArrayList<>();
        int month=new Date().getMonth()+1;
        for(AmDriver amDriver:drivers){
            String driver=amDriver.getDriverNum()+"";
            String name=amDriver.getName();
            AmWork amWork=new AmWork();

            //基础信息
            amWork.setDriver(driver);
            amWork.setName(amDriver.getName());
            amWork.setMonth(month+"");
            amWork.setCar(amDriver.getCarId());

            //白班夜班
            String[] times=amRosterService.selectTime(amDriver.getName(),month+"").split("\\:");
            amWork.setDay(Integer.parseInt(times[0]));
            amWork.setNight(Integer.parseInt(times[1]));

            //加班时长
            amWork.setOver(new BigDecimal(amOverService.getOverTime(name,month+"")));

            //市区长途
            int city=0,trip=0,city_oil=0,last_trivel=0,this_trivel=0;
            double income=0;
            AmDriving amDriving=new AmDriving();
            amDriving.setName(name);
            List<AmDriving> drivings=amDrivingService.selectAmDrivingList(amDriving);
            for(AmDriving amDriving1:drivings){
                if(amDriving1.getDrivingTime().getMonth()+1==month) {
                    city += Integer.parseInt(amDriving1.getChuyuan());
                    trip += Integer.parseInt(amDriving1.getTrip());
                    income += Double.parseDouble(amDriving1.getIncome()) + Double.parseDouble(amDriving1.getTripIncome());
                    city_oil += Integer.parseInt(amDriving1.getJijiu());
                }
            }
            amWork.setIncome(new BigDecimal(income));
            amWork.setCity(new BigDecimal(city));
            amWork.setTrip(trip);
            amWork.setCityOil(city_oil+"");

            //里程
            this_trivel=getMax(drivings,month);
            if(month==1)
                last_trivel=getMax(drivings,12);
            else
                last_trivel=getMax(drivings,month-1);
            amWork.setLastTrivel((long) last_trivel);
            amWork.setThisTrivel((long) this_trivel);
            amWork.setTrivel(this_trivel-last_trivel+"");

            //外地加油
            AmTrip amTrip=new AmTrip();
            amTrip.setMainName(name);
            List<AmTrip> trips=amTripService.selectAmTripList(amTrip);
            double trip_oil=0;
            for(AmTrip amTrip1:trips){
                if(amTrip1.getEndTime().getMonth()+1==month){
                    trip_oil+=Double.parseDouble(amTrip1.getOil());
                }
            }
            amWork.setTripOil(trip_oil+"");
            amWork.setOil(city_oil+trip_oil+"");

            works.add(amWork);
        }

        return works;
    }

    public int getMax(List<AmDriving> drivings,int month){
        int max=0;
        for(AmDriving amDriving:drivings){
            if(amDriving.getDrivingTime().getMonth()+1==month&&max<Integer.parseInt(amDriving.getMile())){
                max=Integer.parseInt(amDriving.getMile());
            }
        }
        return max;
    }

    @GetMapping("/getWork")
    @ResponseBody
    public List<AmWork> getWork(){
        AmWork amWork=new AmWork();
        amWork.setMonth(new Date().getMonth()+1+"");
        return amWorkService.selectAmWorkList(amWork);
    }

}



